/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-12-03
 * Time: 12:22
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Diagnostics;

using tools;

using Labb4.Media.Graphics;
using Labb4.Media.Sfx;

namespace Labb4.Media
{
	/// <summary>
	/// Manages all the media (pictures and sounds) making sure a file is never loaded
	/// twice. Practices "load on play" principle
	/// 
	/// This class implements the Singleton design pattern
	/// </summary>
	public sealed class MediaManager : Manager
	{
		#region members
		
		/// <summary>
		/// The one instance
		/// </summary>
		private static MediaManager					m_Instance = new MediaManager();
		/// <summary>
		/// The pictures, address of the file is key
		/// </summary>
		private Dictionary<string, Picture>			m_Pictures;
		/// <summary>
		/// The sounds, address of the file is key
		/// </summary>
		private Dictionary<string, Audio> 			m_Sounds;
		
		#endregion
		
		#region constructors
		
		/// <summary>
		/// Constructor, initializes members
		/// </summary>
		private MediaManager() :
			base()
		{
			m_Pictures = new Dictionary<string, Picture>();
			m_Sounds = new Dictionary<string, Audio>();
		}
		
		#endregion
		
		#region methods
		
		/// <summary>
		/// Gets a picture, loads it if it's not allready loaded
		/// </summary>
		/// <param name="name">The address to the file</param>
		/// <returns>The picture loaded from the drive</returns>
		public Picture GetPicture(string name)
		{
			Picture picture = null;
			
			if(m_Pictures.ContainsKey(name))
			{
				picture = m_Pictures[name];
			}
			
			if(picture == null)
			{
				picture = new Picture(name);
			}
			
			return picture;
		}
		
		/// <summary>
		/// Adds a picture to the manager
		/// </summary>
		/// <param name="name">The name to associate this picture with</param>
		/// <param name="picture">The picture to add</param>
		public void AddPicture(string name, Picture picture)
		{
			try
			{
				m_Pictures.Add(name, picture);
			}
			catch(Exception)
			{
				Debug.WriteLine("Picture added twice..");
			}
		}
		
		/// <summary>
		/// Gets an audio, loads it if it's not allready loaded
		/// </summary>
		/// <param name="fileName"></param>
		/// <returns></returns>
		public Audio GetAudio(string fileName)
		{
			Audio audio = null;
			
			if(!m_Sounds.ContainsKey(fileName))
			{
				audio = new Audio(fileName, true);
			}
			else
			{
				audio = m_Sounds[fileName];
			}
			
			return audio;
		}
		
		/// <summary>
		/// Adds an audio to the manager
		/// </summary>
		/// <param name="fileName">The name to associate this audio with</param>
		/// <param name="sound">The audio to add</param>
		public void AddAudio(string fileName, Audio sound)
		{
			if(!m_Sounds.ContainsKey(fileName))
			{
				m_Sounds.Add(fileName, sound);
			}
		}
		
		#endregion
		
		#region overrides
		
		/// <summary>
		/// Tears down the class, releasing all audio and image files
		/// </summary>
		protected override void DoTearDown()
		{
			foreach(KeyValuePair<string, Audio> it in m_Sounds)
			{
				it.Value.Release();
			}
			
			foreach(KeyValuePair<string, Picture> it in m_Pictures)
			{
				it.Value.Release();
			}
			
			m_Sounds.Clear();
			m_Pictures.Clear();
		}
		
		#endregion
		
		#region properties
		
		/// <summary>
		/// The one instance (get)
		/// </summary>
		public static MediaManager Instance
		{
			get
			{
				return m_Instance;
			}
		}
		
		#endregion
	}
}
